﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hjt212Parser
{
    public static class DateTimeHelper
    {
        /// <summary>
        /// 返回年初 
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static DateTime YearBegin(DateTime datetime)
        {
            return new DateTime(datetime.Year, 1, 1);
        }
        /// <summary>
        /// 返回年末 
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static DateTime NextYear(DateTime datetime)
        {
            return new DateTime(datetime.Year + 1, 1, 1);
        }
        /// <summary>
        /// 返回季初 
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static DateTime QuarterBegin(DateTime datetime)
        {
            var quarterBegin = datetime.AddMonths(0 - (datetime.Month - 1) % 3).AddDays(1 - datetime.Day);
            return new DateTime(quarterBegin.Year,quarterBegin.Month,1);  //本季度初  
        }

        public static DateTime NextQuarter(DateTime datetime)
        {
            return QuarterBegin(datetime).AddMonths(3);
        }
        public static int Quarter(DateTime datetime)
        {
            return (datetime.Month + 2) / 3;
        }
        /// <summary>
        /// 返回月初 
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static DateTime MonthBegin(DateTime datetime)
        {
            return new DateTime(datetime.Year, datetime.Month, 1);
        }
        public static DateTime NextMonth(DateTime datetime)
        {
            return MonthBegin(datetime).AddMonths(1);
        }

        public static bool IsRecentlyYear(DateTime? dateTime)
        {
            if (dateTime == null) return true;
            var now = DateTime.Now;
            var span = (now - dateTime.Value).TotalDays;
            if (span >= -1 && span < 366)
            {
                return true;
            }
            return false;
        }

    }
}
